package com.mjh.dao;

import java.util.ArrayList;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.dom4j.Element;
import com.mjh.bean.Category;
import com.mjh.util.ResourceUtil;
import com.mjh.util.XMLParser;

public class CategoryConfigDAO {

	public static Logger log = Logger.getLogger(CategoryConfigDAO.class);
	
	public static XMLParser categoryXML;
	
	static {
		categoryXML = new XMLParser(ResourceUtil.getRealPath("com/mjh/config/category.xml"));
	}
	
	private int getMaxCategoryId(){
		return getMaxId(categoryXML.listAllNodeFromRoot(), 0);
	}
	
	private int getMaxSubCategoryId(String parentId){
		return getMaxId(categoryXML.listAllNode(parentId), Integer.parseInt(parentId));
	}	
	
	private int getMaxId(Iterator<Element> it, int defaultMaxId){
		int maxId = 0;
		while(it.hasNext()){
			Element el = (Element)it.next();
			int id = Integer.parseInt(el.attributeValue("id"));
			maxId = Math.max(maxId, id);
		}
		return Math.max(maxId, defaultMaxId);
	}
	
	private void saveChange(){
		categoryXML.wirteXML();
	}
	
	public void addCategory(String categoryName){
		String id = String.valueOf(this.getMaxCategoryId() + 10000);
		Element category = categoryXML.appendNodeToRoot("category");
		category.addAttribute("id", id);
		category.addAttribute("name", categoryName);
		saveChange();
	}
	
	public void addSubCategory(String parentId, String subCategoryName){
		String id = String.valueOf(this.getMaxSubCategoryId(parentId) + 1);
		Element category = categoryXML.appendNodeById(parentId, "artcletype");
		category.addAttribute("id", id);
		category.addAttribute("name", subCategoryName);
		saveChange();
	}
	
	public void updateCategory(String id, String name){
		categoryXML.setAttributeById(id, "name", name);
		saveChange();
	}

	public void delCategory(String id){
		categoryXML.removeNode(id);
		saveChange();
	}
	
	public Category getCategory(Element el){
		Category category = new Category();
		category.setId(el.attributeValue("id"));
		category.setName(el.attributeValue("name"));
		ArrayList<Category> categorys = new ArrayList<Category>();
		Iterator<Element> it = el.elementIterator();
		while(it.hasNext()){
			categorys.add(getCategory((Element)it.next()));
		}
		category.setSubCategory(categorys);
		return category;
	}
	
	public ArrayList<Category> listAllCategory(){
		ArrayList<Category> categorys = new ArrayList<Category>();
		Iterator<Element> it = categoryXML.listAllNodeFromRoot();
		while(it.hasNext()){
			categorys.add(getCategory((Element)it.next()));
		}
		return categorys;
	}
	
	public static void main(String[] args) {
		CategoryConfigDAO dao = new CategoryConfigDAO();
		ArrayList<Category> categorys = dao.listAllCategory();
		for(int i=0;i<categorys.size();i++){
			Category c = (Category)categorys.get(i);
			System.out.println(c.getId() + "  " + c.getName() + "  ");
			ArrayList<Category> cs = c.getSubCategory();
			for(int j=0;j<cs.size();j++){
				Category cc = (Category)cs.get(j);
				System.out.println("  " +cc.getId() + "  " + cc.getName() + "  ");
			}
		}
	}
	
}
